<template>
  <div class="login-container">
    <div class="login-box">
      <!--头像-->
      <div class="image-box">
        <img src="../assets/xjj.jpg" height="200" width="200"/>
      </div>
      <el-form :model="LoginForm" ref="LoginFormRef" :rules="LoginFormRules" class="form-box">
        <el-form-item prop="username">
          <el-input clearable maxlength="6" prefix-icon="el-icon-user" v-model="LoginForm.username" placeholder="姓名"></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input show-password @keyup.enter="login" prefix-icon="el-icon-lock" v-model="LoginForm.password" placeholder="密码"></el-input>
        </el-form-item>
        <el-form-item class="button-box">
          <el-button type="primary" @click="login" >登录</el-button>
          <el-button type="info" @click="resetForm('LoginFormRef')">重置</el-button>
        </el-form-item>
      </el-form>

    </div>
  </div>
</template>
<script>
export default {
  data: function () {
    return {
      LoginForm: {
        username: 'admin',
        password: '123456'
      },
      LoginFormRules: {
        username: [
          { required: true, message: '请输入用户名称', trigger: 'blur' },
          { min: 3, max: 6, message: '长度在 3 到 6 个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 6, max: 10, message: '长度在 6 到 10 个字符', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    onSubmit () {
      console.log('submit!')
    },
    resetForm (val) {
      this.$refs[val].resetFields()
    },
    login () {
      const that = this
      this.$refs.LoginFormRef.validate(val => {
        if (!val) return
        that.$http.post('login', that.LoginForm).then(res => {
          const resData = res.data
          if (parseInt(resData.meta.status) === 200) {
            that.$message({
              showClose: true,
              message: '登录成功 欢迎你 !' + that.LoginForm.username,
              type: 'success'
            })
            window.sessionStorage.setItem('token', resData.data.token)
            // 编程式导航
            that.$router.push('/Home')
          } else {
            that.$message({
              showClose: true,
              message: '登录失败',
              type: 'error'
            })
          }
        }, function () {
          that.$message.error('服务器异常')
        })
      })
    }
  }
}
</script>
<style lang="less" scoped>
  .login-container{
    height: 100%;
    background: #2b4b6b;
  }
  .login-box {
    width: 454px;
    height: 338px;
    background-color: #fff;
    border-radius: 4px;
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);
    .image-box {
      width: 130px;
      height: 130px;
      position: absolute;
      background-color: #eeeeee;
      border-radius: 50%;
      left: 50%;
      transform: translate(-50%, -50%);
      padding: 10px;
      box-shadow: 0 2px 25px #e8e8e8;

      img {
        border-radius: 50%;
        width: 100%;
        height: 100%;
        background-color: #fff;
      }
    }
    .form-box{
      width: 100%;
      padding: 15px 30px 0 30px;
      box-sizing: border-box;
      position: absolute;
      bottom: 0;
      .button-box{
        display: flex;
        justify-content: flex-end;
      }
    }
  }
</style>
